package cn.com.anysdk.starter.config.sms;

import cn.com.anysdk.sms.api.ISmsService;
import cn.com.anysdk.sms.config.AliyunSmsConfig;
import cn.com.anysdk.sms.impl.AliyunSmsService;
import com.aliyun.dysmsapi20170525.Client;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(AliyunSmsProperties.class)
@ConditionalOnProperty(prefix = "anysdk.sms.aliyun", name = "enable",havingValue = "true", matchIfMissing = true)
public class AliyunSmsAutoConfiguration {

    @Bean
    public AliyunSmsConfig aliyunSmsConfig(AliyunSmsProperties properties) {
        return AliyunSmsConfig.builder()
                .accessKeyId(properties.getAccessKeyId())
                .accessKeySecret(properties.getAccessKeySecret())
                .region(properties.getRegionId())
                .signName(properties.getSignName())
                .defaultTemplateId(properties.getDefaultTemplateId())
                .endpoint(properties.getEndpoint())
                .build();
    }

    @Bean
    public Client client(AliyunSmsConfig config) throws Exception {
        return config.createClient();
    }

    @Bean
    public ISmsService smsService(AliyunSmsConfig config) throws Exception {
        return new AliyunSmsService(config);
    }

}
